<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once BASEPATH.'../data_manager/DataManager.php';
class Administration extends CI_Controller {


	private $dm_profile = 'wings_admin';
    private $controller = '';
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	function __construct() {
		parent::__construct();

		$this->controller = strtolower(__CLASS__);
	}


	function create() {
		$dm = new DataManager($this->dm_profile);
		$dm->setMode('single');
		if ($dm->loadData() === false) {
			var_dump($dm->getErrors());
			exit;
		}

		$data['titles'] = $dm->getTitles();
		$data['fields'] = $dm->getFields();
		$data['fields'] = $data['fields'][0];
		$data['controller'] = $this->controller;

		$data['page'] = $this->controller.'/edit';

		$this->load->view($this->controller.'/main_tpl', $data);
	}
	
	function delete() {

		$id = (int) $this->uri->segment(3, 0);
				
		//$query = $this->db->query('update bkw_wings set rec_status=0 where wing_id='.$id);
					
				
		$dm = new DataManager($this->dm_profile);
		if ($dm->delete($id) === false) {
			var_dump($dm->getErrors());
			exit;
		}
		
		redirect('/'.$this->controller.'/wing_list/1/deleted');
	}
	
	function edit() {
		$id = (int) $this->uri->segment(3, 0);
		
		$dm = new DataManager($this->dm_profile);
		if ($id) {
			$dm->setKeyValue($id);
		}
		$dm->setMode('single');
		if ($dm->loadData() === false) {
			var_dump($dm->getErrors());
			exit;
		}

		$data['titles'] = $dm->getTitles();
		$data['fields'] = $dm->getFields();
		$data['fields'] = $data['fields'][0];
		$data['controller'] = $this->controller;
		
		$data['page'] = $this->controller.'/edit';
		
		$this->load->view($this->controller.'/main_tpl', $data);
	}
	
	function index() {
		//$this->wing_list();

  
		
		
		$this->load->view($this->controller.'/main_tpl', $data);


	}





	function member_list() {
		
		$query = $this->db->query('SELECT wing_alias,wing_name from  bkw_wings where rec_status=1');
		return $query->result();

		$page = (int) $this->uri->segment(3, 1);
		$page = ($page?$page:1);
		$items_per_page = 7;
		
		$dm = new DataManager($this->dm_profile);
		$dm->setItemsPerPage($items_per_page);
		$dm->setPage($page);
		$dm->setMode('list');
		$dm->setOrderingField('member_id', 0);
		if ($dm->loadData() === false) {
			var_dump($dm->getErrors());
			exit;
		}

		$data['pages_count'] = $dm->getPagesCount();
		
		$message = $this->uri->segment(4);
		switch($message) {
			case 'deleted':
				$data['message'] = 'Item deleted.';
				break;
			case 'saved':
				$data['message'] = 'Item saved.';
				break;
			default:
				$data['message'] = '';
		}

		$data['titles'] = $dm->getTitles();
		$data['fields'] = $dm->getFields();
		$data['controller'] = $this->controller;
		
		$data['page'] = $this->controller.'/list';
		
		$this->load->view($this->controller.'/main_tpl', $data);
	}
	
function login() {
	
	$data['error'] ="";
		if($_POST){

			if($_POST["email"]=="admin@bkwings.com" && $_POST["password"]=="admin123!"){

				$data['page'] = $this->controller.'/wing_list';
		
				

			}else{

				$data['page'] = $this->controller.'/login';
				$data['error'] = "Invalid Credentials. Please check";

			}

		}else{

			$data['page'] = $this->controller.'/login';
		
		}

		$this->load->view($this->controller.'/main_tpl', $data);
	}


	
	function wing_list() {
		$page = (int) $this->uri->segment(3, 1);
		$page = ($page?$page:1);
		$items_per_page = 7;
		
		$dm = new DataManager($this->dm_profile);
		$dm->setItemsPerPage($items_per_page);
		$dm->setPage($page);
		$dm->setMode('list');
		$dm->setOrderingField('wing_id', 0);
		if ($dm->loadData() === false) {
			var_dump($dm->getErrors());
			exit;
		}

		$data['pages_count'] = $dm->getPagesCount();
		
		$message = $this->uri->segment(4);
		switch($message) {
			case 'deleted':
				$data['message'] = 'Item deleted.';
				break;
			case 'saved':
				$data['message'] = 'Item saved.';
				break;
			default:
				$data['message'] = '';
		}

		$data['titles'] = $dm->getTitles();
		$data['fields'] = $dm->getFields();
		$data['controller'] = $this->controller;
		
		$data['page'] = $this->controller.'/list';
		
		$this->load->view($this->controller.'/main_tpl', $data);
	}
	
	function save() {
		$data = $this->input->post();
		$dm = new DataManager($this->dm_profile);
	
		if ($dm->save($data) === false) {
			var_dump($dm->getErrors());
			exit;
		}
			
		redirect('/'.$this->controller);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */